package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.DepartmentsMapper;
import com.soft.springhrms.mapper.EmployeesMapper;
import com.soft.springhrms.pojo.Departments;
import com.soft.springhrms.pojo.Positions;
import com.soft.springhrms.service.PositionsService;
import com.soft.springhrms.mapper.PositionsMapper;
import com.soft.springhrms.utils.RedisUtil;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DELL
 * @description 针对表【positions】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:18
 */
@Service
public class PositionsServiceImpl extends ServiceImpl<PositionsMapper, Positions>
        implements PositionsService {
    @Autowired
    private DepartmentsMapper departmentMapper;
    @Autowired
    EmployeesMapper employeesMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public List<Positions> getAllPositions() {
        List<Positions> positionsList= (List<Positions>) redisUtil.get("positionsList");
        if(positionsList==null ||positionsList.isEmpty()){
            positionsList= list();
        try {
            for (Positions position : positionsList) {
                position.setPositionCount(employeesMapper.countByPosition(position.getId()));
                position.setDepartmentName(departmentMapper.selectById(position.getDepartmentId()).getName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
            System.out.println("Redis中没有，从数据库获取");
            redisUtil.set("positionsList", positionsList,60*60);
        }
        return positionsList;
    }

    @Override
    public Positions getPositionById(Integer id) {
        Positions position = (Positions) redisUtil.get("position+"+id);
        if(position==null){
            position= getById(id);
        if (position != null) {
            Departments department = departmentMapper.selectById(position.getDepartmentId());
            position.setDepartmentName(department.getName());
            position.setDepartmentId(department.getId());
        }
            redisUtil.set("position+"+id,position,60*60);
        }
        return position;
    }

}




